//
//  OpinionViewController.m
//  YunBroker
//
//  Created by uplooking on 2017/2/7.
//  Copyright © 2017年 RenLong. All rights reserved.
//

#import "OpinionViewController.h"

@interface OpinionViewController ()
<
YYTextViewDelegate,
UITextFieldDelegate
>
{
    YYTextView *textView;
    UITextField * phoneTextField;
}

@end

@implementation OpinionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"意见反馈";
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    [self initView];
    
}

- (void)backAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    [self setBackLeftBarButtonItemAction:@selector(backAction:)];
    
}


-(void)initView{
    
    UIView * topView = [[UIView alloc]init];
    [topView setBackgroundColor:HexRGBA(0xf5f5f5, 1)];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(45 * kAutoScanleX);
    }];
    
    
    UIImageView * imageView = [[UIImageView alloc]init];
    [topView addSubview:imageView];
    [imageView setImage:[UIImage imageNamed:@"icon_quanbu_pinglun"]];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(topView);
        make.left.equalTo(topView).offset(15 * kAutoScanleX);
        make.size.mas_equalTo(CGSizeMake(6 * kAutoScanleX, 15 * kAutoScanleX));
    }];
    
    
    
    UILabel * clabel= [[UILabel alloc]init];
    [clabel setTextColor:HexRGBA(0x000000, 1)];
    [clabel setText:@"反馈内容"];
    [clabel setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [clabel setTextAlignment:NSTextAlignmentLeft];
    [topView addSubview:clabel];
    [clabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(15 * kAutoScanleX);
        make.centerY.equalTo(topView);
    }];
    
    
    UIView * item = [[UIView alloc]init];
    [self.view addSubview:item];
    [item setBackgroundColor:[UIColor whiteColor]];
    [item mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(200 * kAutoScanleX);
    }];
    
    textView = [[YYTextView alloc]init];
    [textView setBackgroundColor:[UIColor whiteColor]];
    [item addSubview:textView];
    [textView setDelegate:self];
    [textView setPlaceholderText:@"请您写下宝贵的意见~"];
    [textView setReturnKeyType:UIReturnKeyDone];
    [textView setFont:[UIFont systemFontOfSize:15 *kAutoScanleX]];
    [textView setPlaceholderFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [textView setTextColor:HexRGBA(0x000000, 1)];
    [textView setPlaceholderTextColor:HexRGBA(0xb3b5b8, 1)];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(item).insets(UIEdgeInsetsMake(15 * kAutoScanleX, 15 * kAutoScanleX, 15 * kAutoScanleX, 15 * kAutoScanleX));
    }];
    
    
    UIView * midView = [[UIView alloc]init];
    [midView setBackgroundColor:HexRGBA(0xffffff, 1)];
    [self.view addSubview:midView];
    [midView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(item.mas_bottom).offset(10 * kAutoScanleX);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(55 * kAutoScanleX);
    }];
    
    
    phoneTextField=[[UITextField alloc]init];
    [phoneTextField setDelegate:self];
    [midView addSubview:phoneTextField];
    [phoneTextField setKeyboardType:UIKeyboardTypePhonePad];
    [phoneTextField setReturnKeyType:UIReturnKeyDone];
    [phoneTextField setFont:[UIFont systemFontOfSize:15 * kAutoScanleX]];
    [phoneTextField setValue:HexRGBA(0xa4a4a4, 1) forKeyPath:@"_placeholderLabel.textColor"];
    [phoneTextField setPlaceholder:@"请输入您的联系方式"];
    [phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(midView);
        make.left.equalTo(midView).offset(15 * kAutoScanleX);
        make.right.equalTo(midView);
        make.height.mas_equalTo(50 * kAutoScanleX);
    }];
    
    
    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kScreenWidth, 40)];
    customView.backgroundColor = [UIColor whiteColor];
    phoneTextField.inputAccessoryView = customView;
    
    UIButton *rightBtn=[[UIButton alloc]init];
    [rightBtn addTarget:self action:@selector(finishBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightBtn setTitle:@"完成" forState:UIControlStateNormal];
    [customView addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(customView);
        make.right.equalTo(customView).offset(-15);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    
    UIView *line=nil;
    
    line=[[UIView alloc]initWithFrame:CGRectMake(0, 0, customView.current_w, 0.5)];
    [customView addSubview:line];
    [line setBackgroundColor:HexRGBA(0xd3d5d5, 1)];
    
    line=[[UIView alloc]initWithFrame:CGRectMake(0, 39.5, customView.current_w, 0.5)];
    [customView addSubview:line];
    [line setBackgroundColor:HexRGBA(0xd3d5d5, 1)];
    
    UIButton * registerBtn=[[UIButton alloc]init];
    [registerBtn setBackgroundColor:HexRGBA(0xb99c6d, 1)];
    [registerBtn addTarget:self action:@selector(exitBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [registerBtn.layer setMasksToBounds:YES];
    [registerBtn.layer setCornerRadius:8.0 * kAutoScanleX];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setTitle:@"提交" forState:UIControlStateNormal];
    [registerBtn.titleLabel setFont:[UIFont systemFontOfSize:16 * kAutoScanleX]];
    [self.view addSubview:registerBtn];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(midView.mas_bottom).offset(20 * kAutoScanleX);
        make.height.mas_equalTo(45 * kAutoScanleX);
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(576/2.0 * kAutoScanleX);
    }];
    
}

#pragma mark -- 关闭键盘
- (void)finishBtnAction:(UIButton *)sender{
    
    [self.view endEditing:YES];
    
}

#pragma mark--意见反馈
-(void)exitBtnAction:(UIButton *)sender{
    
    NSString * text = textView.text;
    if (text.length == 0) {
        
        return [self showHint:@"请填写您的意见"];
    }
    
    if (phoneTextField.text.length == 0) {
        
        return [self showHint:@"请填写您的电话号码"];
    }
    
    WS(weakSelf);
    [self showHudInView:self.view hint:@""];
    [HttpRequestHelper requestApiFeedbackMobile:phoneTextField.text msg:textView.text Block:^(NSString *msg) {
        [weakSelf hideHud];
        if (REQUEST_SUCCESS) {
            [weakSelf showHint:@"提交成功"];
            [weakSelf backAction:nil];

        }else{
            [weakSelf showHint:msg];
        }
    }];

    
}

#pragma mark -- 代理
- (BOOL)textView:(YYTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString:@"\n"])
    {
        
        [ApplicationDelegate.window endEditing:YES];
        
        return NO;
    }
    
    
    return YES;
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [self.view endEditing:YES];
    
    return YES;
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    [ApplicationDelegate.window endEditing:YES];
}


@end
